// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "flare/example/custom_protocol/service.h"

#include <memory>
#include <string>
#include <vector>

#include "flare/example/custom_protocol/naive_message.h"

namespace example::naive_proto {

const flare::experimental::Uuid& Service::GetUuid() const noexcept {
  // Generated by `uuidgen` on my Mac.
  static constexpr flare::experimental::Uuid kUuid(
      "96316D52-DEF7-45D7-9616-225E361CCD55");
  return kUuid;
}

bool Service::Inspect(const flare::Message&, const flare::Controller&,
                      InspectionResult*) {
  return true;
}

bool Service::ExtractCall(const std::string& serialized,
                          const std::vector<std::string>& serialized_pkt_ctxs,
                          ExtractedCall* extracted) {
  FLARE_LOG_ERROR_ONCE("{}: Not implemented.", __func__);
  return false;
}

flare::StreamService::ProcessingStatus Service::FastCall(
    std::unique_ptr<flare::Message>* request,
    const flare::FunctionView<std::size_t(const flare::Message&)>& writer,
    Context* ctx) {
  FLARE_CHECK((*request)->GetType() == flare::Message::Type::Single);
  auto req = flare::cast<NaiveMessage>(**request);
  writer(NaiveMessage(req->GetCorrelationId(), req->Body()));
  return ProcessingStatus::Processed;
}

flare::StreamService::ProcessingStatus Service::StreamCall(
    flare::AsyncStreamReader<std::unique_ptr<flare::Message>>* input_stream,
    flare::AsyncStreamWriter<std::unique_ptr<flare::Message>>* output_stream,
    Context* context) {
  FLARE_CHECK(!"Not implemented.");
}

}  // namespace example::naive_proto
